//
//  CompanyMineCollectViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/31.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyMineCollectViewController.h"
#import "ConpanyLookMeCell.h"
#import "WorkerModel.h"
//#import "JobDetailViewController.h"
#import "WorkerResumeDetailViewController.h"
#import "CompanyJobModel.h"

@interface CompanyMineCollectViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *itemDataArray;
    
    UITableView *collectTableview;
    
    NSInteger currentPage;
    
    BOOL noNetOrData; // yes:没有网 NO：没有数据
}
@property (nonatomic, copy) NSString *job_id; // 职位
@end

@implementation CompanyMineCollectViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"人才收藏";
    
    itemDataArray = [NSMutableArray arrayWithCapacity:0];
    currentPage = 1;
    noNetOrData = YES;
    
    [self initUI];
    [self loadDataForJobID];
    [self loadData];
}
- (void)loadData {
    self.noDataView.hidden = YES;
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:[NSString stringWithFormat:@"%ld", (long)currentPage] forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:Company_GetFavoriteResume_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            for (NSDictionary *dic in data) {
                WorkerModel *model = [[WorkerModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                model.type = WorkerCellTypeOfCollect;
                model.collectType = CollectTypeOfCompany;
                [self->itemDataArray addObject:model];
            }
            self.noDataView.hidden = YES;
            [self->collectTableview.mj_footer endRefreshing];
            [self->collectTableview reloadData];
        }
        if (self->itemDataArray.count) {
        } else {
            self->noNetOrData = NO;
            self.noDataView.hidden = NO;
            [self.noDataView vcNilStatusType:VCNilStatusTypeOfWorkerCollect superView:self.view];
        }
        SVPHUDDismiss;
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
        self->noNetOrData = YES;
        [self.noDataView vcNilStatusType:VCNilStatusTypeOfNoNet superView:self.view];
    }];
}
- (void)initUI {
    collectTableview = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height) target:self style:UITableViewStyleGrouped];
    collectTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    collectTableview.backgroundColor = COLOR_OF_WHITE;
    collectTableview.rowHeight = 118*Scale;
    [self.view addSubview:collectTableview];
    
    __weak __typeof(self)weakSelf = self;
    collectTableview.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self->currentPage++;
        [self->collectTableview.mj_footer beginRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (itemDataArray.count) {
        return itemDataArray.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ConpanyLookMeCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[ConpanyLookMeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
    }
//    [cell reloadData];
    WorkerModel *model = itemDataArray[indexPath.row];
    [cell reloadDataModel:model];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.job_id && self.job_id.length) {
        WorkerModel *model = itemDataArray[indexPath.row];
        WorkerResumeDetailViewController *WRDVC = [[WorkerResumeDetailViewController alloc] init];
        WRDVC.id = model.resume_id;
        WRDVC.job_id = self.job_id;
        WRDVC.showChatSettingFlag = NO;
        WRDVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:WRDVC animated:YES];
    } else {
        SVPHUDError(@"您还没有开放中的职位");
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001;
}
- (void)loadDataForJobID {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:@"1" forKey:@"status"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"1" forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:Company_GetJob_Lists_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                if ([data count]) {
                    [DataToolsControl sharedManager].validJob = YES;
                    NSDictionary *dicc = data[0];
                    CompanyJobModel *model = [[CompanyJobModel alloc] init];
                    [model setValuesForKeysWithDictionary:dicc];
                    
                    self.job_id = model.id;
                }
            }
        }
    } failurBlock:^(NSError * _Nonnull error) {
//        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)noNetButtonTap {
    if (noNetOrData) {
        [self loadData];
    } else {
        [self.navigationController popViewControllerAnimated:NO];
        UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        tabBarController.selectedIndex = 0;
    }
}

@end
